﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NIKE_Project.DAL;

namespace NIKE_Project.BLL
{
    public class SalesanServer
    {
        //调用DBHelper类
        DBHelper db = new DBHelper();

        //定义DataSet全局变量
        public DataSet ds;
        //员工角色
        public DataSet Role()
        {
            string Sqlxlk = string.Format("select distinct Role from Salesan;");
            ds = db.getDataSet(Sqlxlk);
            return ds;
        }
        //员工管理
        public DataSet SalesanLIst()
        {
            string Sqldv5 = string.Format("select SalesmanID,SalesmanName,Mobile,Gender,BaseSalary,CommissionRate,Role from Salesan;");
            ds = db.getDataSet(Sqldv5);
            return ds;
        }
        //删除
        public DataSet SalesanDelect(string id)
        {
           string Sqlstr = string.Format("delete Salesan where SalesmanID='{0}' ", id);
            //ds = db.getDataSet(Sqlstr);
            int rows = db.zsg(Sqlstr);
            if (rows > 0)
            {

                //MessageBox.Show("新增成功！");
                //DataGridView5刷新
                string Sqldv5 = string.Format("select SalesmanID,SalesmanName,Mobile,Pwd,Gender,BaseSalary,CommissionRate,Role from Salesan;");
                ds = db.getDataSet(Sqldv5);

            }
            else
            {
                throw new Exception();
            }
            return ds;

        }
        private RegisterServer server = new RegisterServer();

        public DataSet SalesanAdd(string name, string phone, string gender, string basesalar,  string deduct, string pwd, string role)
        {
            pwd = server.GenerateMD5(pwd);
            string Sqlinsert = "";
            //判断是否有提成比例
            if (deduct == "")
            {
                deduct = "null";
                Sqlinsert = string.Format(@"insert into Salesan(SalesmanName,Mobile,Pwd,Gender,BaseSalary,CommissionRate,Role) Values ('{0}','{1}','{2}','{3}','{4}',{5},'{6}')",
            name, phone, pwd, gender, basesalar, deduct, role);
            }
            else
            {
                Sqlinsert = string.Format(@"insert into Salesan(SalesmanName,Mobile,Pwd,Gender,BaseSalary,CommissionRate,Role) Values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
                name, phone, pwd, gender, basesalar, deduct, role);
            }
            int rows = db.zsg(Sqlinsert);
            if (rows > 0)
            {
               
                //MessageBox.Show("新增成功！");
                //DataGridView5刷新
                string Sqldv5 = string.Format("select SalesmanID,SalesmanName,Mobile,Pwd,Gender,BaseSalary,CommissionRate,Role from Salesan;");
                ds = db.getDataSet(Sqldv5);

            }
            else
            {
                throw new Exception();
            }

            return ds;
        }

        public DataSet SalesanEidt(string name, string phone, string gender, string basesalar, string deduct, string role, string ID)
        {
            string Sqlupdate = "";
            if (deduct == "")
            {
                deduct = "null";
                Sqlupdate = string.Format(@"update Salesan set SalesmanName='{0}',Mobile='{1}',Gender='{2}',BaseSalary='{3}',CommissionRate={4},Role='{5}' where SalesmanID='{6}'",
                                                      name, phone, gender, basesalar, deduct, role, ID);
            }
            else
            {
                Sqlupdate = string.Format(@"update Salesan set SalesmanName='{0}',Mobile='{1}',Gender='{2}',BaseSalary='{3}',CommissionRate='{4}',Role='{5}' where SalesmanID='{6}'",
                                                      name, phone, gender, basesalar, deduct, role, ID);
            }
            int rows = db.zsg(Sqlupdate);
            if (rows > 0)
            {
                //MessageBox.Show("修改成功！");
                //DataGridView5刷新
                string Sqldv5 = string.Format("select * from Salesan;");
                ds = db.getDataSet(Sqldv5);

            }
            else
            {
                //MessageBox.Show("修改失败！");
                throw new Exception();
            }

            return ds;
        }


    }
}
